package Motor;
import java.util.Random; 

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotOpenException;


public class ListaCartas {
	
	private static final Exception RecordStoreEmptyException = null;
	private int n;
	private Carta[] l;
	
	public ListaCartas(){
		setN(0);
		setList(new Carta[0]);
	}
	
	public ListaCartas(int n){
		int i=0;
		this.setN(n);
		setList(new Carta[n]);
		while (i<n){
			getList()[i]=null;
			i++;
		}
	}
	
	public void insertar(Carta c){
		int i=0;
		Carta[] aux=new Carta[getN()+1];
		while (i<getN()){
			aux[i]=getList()[i];
			i++;
		}
		aux[getN()]=c;
		setList(aux);
		setN(getN() + 1);
	}
	
	public Carta retirarSuperior(){
		int i=1;
		Carta c=null;
		Carta[] aux=null;
		if (getN()!=0){
			c=getList()[0];
			aux=new Carta[getN()-1];
			while (i<getN()){
				aux[i-1]=getList()[i];
				i++;
			}
			setN(getN() - 1);
			if (getN()!=0){
				setList(aux);
			}else{
				setList(null);
			}
		}
		return c;
	}
	public Carta verCarta(int n){
		Carta c=null;
		if (n>=0 && n<=getList().length){
			c=getList()[n];
		}
		return c;
	}
	
	public int posCarta(int id){
		boolean b=false;
		int i=0;
		while (i<getN() && !b){
			b=getList()[i].getId()==id;
			i++;
		}
		i--;
		if (b){
			return i;
		}else{
			return -1;
		}
	}
	
	public Carta buscarCarta(int id){
		Carta c=null;
		int i=0;
		while (i<getN() && c==null){
			if (getList()[i].getId()==id){
				c=getList()[i];
			}
			i++;
		}
		i--;		
		return c;
	}
	
	public Carta eliminarCarta(int n){
		Carta c=getList()[n];
		Carta[] aux=new Carta[this.getN()-1];
		int i=0;
		while (i<n){
			aux[i]=getList()[i];
			i++;			
		}
		i++;
		while(i<getList().length){
			aux[i-1]=getList()[i];
			i++;
		}
		return c;
	}
	
	public Carta eliminarCartaId(int id){
		Carta c=null;
		Carta[] aux=new Carta[getList().length-1];
		int i=0,pos;
		//Elimino la primera aparicion
		pos=posCarta(id);
		if (pos!=-1){
			while(i<pos){
				aux[i]=getList()[i];
				i++;
			}
			i++;
			while(i<getN()){
				aux[i-1]=getList()[i];
			}
			setList(aux);
		}
		return c;
	}
	
	public int numRepet(Carta c){
		int s=0,i=0;
		while(i<getN()){
			if(c.getId()==getList()[i].getId()){
				s++;
			}
			i++;
		}
		return s;
	}
	
	private void mezclar(){
		Random r=new Random();
        int incA = r.nextInt(8)+1;
        int longitud=this.getList().length;
        int a=0;
        int b=longitud/2;
        Carta caux; 
        while(b<longitud){
            caux=getList()[a];
            getList()[a]=getList()[b];
            getList()[b]=caux;
            b++;
            a=(a+incA)% longitud;
        }
    }
    
    public void barajear(){
    	Random r=new Random();
        int iter = r.nextInt(14)+1;
        int i=0;
        while(i<=iter){
            this.mezclar();
            i++;
        }
    }
    
    public void almacenar(RecordStore rs){
    	int i=0;
    	while(i<this.getN()){
    		this.getList()[i].almacenar(rs);
    		System.out.println("Phone Memory "+Runtime.getRuntime().totalMemory()+"");
			System.out.println("Free Memory(Phone) "+Runtime.getRuntime().freeMemory()+"");
    		i++;
    	}
    }
    
    static public void cargar(RecordStore rs, ListaCartas lc)throws Exception{
    	int i=1;
    	Carta c=null;
    	lc= new ListaCartas();
    	try {
    		System.out.println("Numero registros: "+rs.getNumRecords());
			if(rs.getNumRecords()==0){Exception e=RecordStoreEmptyException;
			throw e;}; 
    		int max=rs.getNumRecords();			
			while(i<=max){
				System.out.println("cargar carta");
				c=c.cargar(rs, i);
				lc.insertar(c);
				i++;
				System.out.println("carta insertada");
			}
		} catch (RecordStoreNotOpenException e) {}
    }

	public int getN() {
		return n;
	}

	public void setN(int n) {
		this.n = n;
	}

	public Carta[] getList() {
		return l;
	}

	public void setList(Carta[] l) {
		this.l = l;
	}

	public ListaCartas copiar() {
		// TODO Auto-generated method stub
		ListaCartas aux=new ListaCartas();
		aux.n=n;
		aux.l=new Carta[n];
		for(int i=0;i<this.l.length;i++){
			if (l[i]==null){
				aux.l[i]=null;
			}else{
				aux.l[i]=l[i].copiar();
			}
		}
		return aux;
	}
}
